var overlay = {
  colorIndex: null,
  to:null,

	openSideBar: function(){
		if(!overlay.isSidebarOpen()){
   		toggleSidebar('viewLaSuliSidebar', true);
  		document.getElementById("sidebar").contentWindow.location = "chrome://lasuli/content/lasuliSidebar.xul";
  	}
	},

	toggleLaSuliSidebar: function(){
	  toggleSidebar('viewLaSuliSidebar');
	},

	isSidebarOpen: function(){
		return (document.getElementById("viewLaSuliSidebar").getAttribute("checked") == "true");
	},

	doPopupShowing: function(){
	  debug("[lasuliOverlay.js::doPopupShowing]",1);
	},

	doOpenCreateItemDialog: function(){
	  var currentUri = getCurrentUri();
	  debug("[lasuliOverylay.js::doOpenCreateItemDialog] currentUri:" + currentUri, 4);
	  var stringBundle = document.getElementById("sbLasuli");
	  if(typeof(currentUri) != "string" || currentUri.trim() == "" || (currentUri.substring(0, "about:".length) == "about:")){
	    alert(stringBundle.getString("lasuliOverlay.error.urlisnull"));
	    return;
	  }
    openDialog("chrome://lasuli/content/createItem.xul", "dlg-create-item", "centerscreen=yes, close=no");
  },

  /**
   * User click the context menu in the web page to highlight a fragment.
   * However, the action will only be allowed when the lasuli sidebar is opened
   * and the viewpoint is selected.
   */
  doHighlightMenuClick: function(evt){
    var stringBundle = document.getElementById("sbLasuli");
    if(!this.isSidebarOpen()){
      if(confirm(stringBundle.getString("lasuliOverlay.info.openLasuli")))
        this.openSideBar();
      return;
    }
    var sidebarWindow = document.getElementById("sidebar").contentWindow;
    var viewpointUri = sidebarWindow.sidebarGui.getSelectedPanelViewpointUri();
    if(viewpointUri == null){
      alert(stringBundle.getString("lasuliOverlay.info.viewpointUnOpened"));
      return;
    }
    var targetId = evt.target.getAttribute("id");
    colorIndex = parseInt(targetId.substr(-1, 1))-1;
    sidebarWindow.jsContextMenu.doContextMenuAddFragment(colorIndex);
  }
}